<script setup>
import { ElMessage } from 'element-plus'
import { getFeedbackListAPI, deleteFeedbackAPI } from '@/api/leaveword'
import { useInitTable } from '@/hooks/useTable'

// 处理响应结果
const handleSuccess = (result) => {
  tableData.value = result.rows.map((item) => {
    item.btnLoading = false
    return item
  })
  total.value = result.count
}
// 列表 | 分页 | 搜索 | 多选 | 删除
const tableOptions = {
  params: {
    page: 1,
    pageSize: 10,
    type: 2,
    order: 'DESC'
  },
  getDataAPI: getFeedbackListAPI,
  onSuccess: handleSuccess
}
const { params, tableData, tableLoading, total, getData, handleSearch, handleIndex } =
  useInitTable(tableOptions)

// 删除反馈
const handleDelete = async (row) => {
  row.btnLoading = true
  try {
    await deleteFeedbackAPI({ type: params.type, ids: row.leaveword_id })
    ElMessage.success('删除成功')
    getData()
  } finally {
    row.btnLoading = false
  }
}

// 排序
const handleSortChange = ({ order }) => {
  if (!order) return
  if (order === 'descending') {
    params.order = 'DESC'
  } else {
    params.order = 'ASC'
  }
  getData()
}
</script>

<template>
  <layout-card>
    <!-- 搜索区域 -->
    <el-form class="search-container" :model="params" inline>
      <el-form-item label="反馈类型:">
        <el-select class="select" v-model="params.type" placeholder="选择反馈类型">
          <el-option label="举报" :value="2" />
          <el-option label="喜欢" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item class="search-btn">
        <el-button type="primary" @click="handleSearch" icon="Search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      sortable="custom"
      :default-sort="{ prop: 'feedback_count', order: 'descending' }"
      :sort-orders="['descending', 'ascending']"
      @sort-change="handleSortChange"
    >
      <el-table-column type="index" label="序号" width="55" :index="handleIndex" />
      <el-table-column label="留言id" prop="leaveword_id" />
      <el-table-column label="签名" show-overflow-tooltip>
        <template #default="{ row }">{{ row.leaveword.name }}</template>
      </el-table-column>
      <el-table-column label="留言内容" show-overflow-tooltip>
        <template #default="{ row }">{{ row.leaveword.content }}</template>
      </el-table-column>
      <el-table-column label="反馈个数" prop="feedback_count" sortable="custom" />
      <el-table-column label="操作" fixed="right" width="140">
        <template #default="{ row }">
          <el-popconfirm
            width="180"
            title="确定要删除此条留言的反馈吗？"
            @confirm="handleDelete(row)"
          >
            <template #reference>
              <el-button
                v-permission="'leaveword:feedback:deleteFeedback'"
                :loading="row.btnLoading"
                type="primary"
                link
                >删除</el-button
              >
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.pageSize"
        :page-sizes="[10, 50, 80, 100]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSearch"
        @current-change="getData"
      />
    </div>
  </layout-card>
</template>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 10px;
  .select {
    width: 130px;
  }
  &.search-btn {
    margin-right: 0;
  }
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: end;
}
</style>
